<template>
    <div class="app-apptainer">
        <h2 style="text-align: center;">发布新课程</h2>

        <el-steps :active="1" process-status="wait" align-center style="margin-bottom: 40px;">
            <el-step title="填写课程基本信息"/>
            <el-step title="创建课程大纲"/>
            <el-step title="最终发布"/>
        </el-steps>
    
        <el-form style="margin-right:100px;" label-width="120px">
            <el-form-item label="课程标题">
                <el-input v-model="courseInfo.title" placeholder=" 示例：机器学习项目课：从基础到搭建项目视频课程。专业名称注意大小写"/>
            </el-form-item>

            <el-form-item label="总课时">
                <el-input-number :min="0" v-model="courseInfo.lessonNum" controls-position="right" placeholder="请填写课程的总课时数"/>
            </el-form-item>

             <el-form-item label="讲师">
                <el-select v-model="courseInfo.teacherId" placeholder="选择讲师">
                    <el-option 
                        v-for="teacher in teacherList" 
                        :key="teacher.id"
                        :label="teacher.name"
                        :value="teacher.id">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="一级课程">
                <el-select v-model="courseInfo.subjectParentId" placeholder="选择一级课程" @change="subjectLevelOneChanged">
                    <el-option 
                        v-for="subject in subjectOneList" 
                        :key="subject.id"
                        :label="subject.title"
                        :value="subject.id">
                    </el-option>
                </el-select>

                <el-select v-model="courseInfo.subjectId" placeholder="选择二级课程">
                    <el-option 
                        v-for="subject in subjectTwoList" 
                        :key="subject.id"
                        :label="subject.title"
                        :value="subject.id">
                    </el-option>
                </el-select>

            </el-form-item>


            <!-- 课程简介 TODO -->
            <!-- 课程简介-->
            <el-form-item  label="课程简介">
                <tinymce :height="300" v-model="courseInfo.description"/>
            </el-form-item>

            <!-- 课程封面 TODO -->
            <!-- 课程封面-->
            <el-form-item label="课程封面">

                <el-upload
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload"
                    :action="BASE_API+'oss/client/policy'"
                    class="avatar-uploader">
                    <img style="width:600px;height:350px;" :src="courseInfo.cover">
                </el-upload>

            </el-form-item>


            <el-form-item label="课程价格">
                <el-input-number :min="0" v-model="courseInfo.price" controls-position="right" placeholder="免费课程请设置为0元"/> 元
            </el-form-item>

            <el-form-item>
                <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">{{btnName}}</el-button>
            </el-form-item>
        </el-form>

    </div>
</template>

<script>
import Tinymce from '@/components/Tinymce' //引入组件
import subject from '@/api/edu/subject'
import teacher from '@/api/edu/teacher'
import course from '@/api/edu/course'

export default {
    //声明组件
    components: { Tinymce },
    data() {
        return {
            saveBtnDisabled:false,
            courseInfo:{
                subjectId: '',//二级分类id
                subjectParentId:'',//一级分类id
                teacherId: '',
                cover: '/static/icon.jpg',
                //标题
                title:'',
                //总课时
                lessonNum:0,
                //简介
                description:'',
                //价格
                price:0,
            },
            btnName:'',
            teacherList:[],//封装所有的讲师
            subjectOneList:[],//一级分类
            subjectTwoList:[],//二级分类

            BASE_API: process.env.BASE_API // 接口API地址
        }
    },
    methods:{
        // 提交
        saveOrUpdate(){
            (this.btnName == "保存并下一步"?
            course.save(this.courseInfo):
            course.update(this.courseInfo))
            .then(response=>{
                //提示
                this.$message({
                    type: 'success',
                    message: '添加课程信息成功!'
                });
                //跳转到第二步
                this.$router.push({path:'/course/chapter/'+response.data.id})
            })
            
        },
        //上传封面成功调用的方法
        handleAvatarSuccess(res, file) {
            console.log("url",res)
            if(res.success){
                this.courseInfo.cover = res.data.url
            }
        },
        //上传之前调用的方法
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg'
            const isLt2M = file.size / 1024 / 1024 < 2

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!')
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!')
            }
            return isJPG && isLt2M
        },
        // 一级课程选择后触发的方法
        subjectLevelOneChanged(value){
            //每次选择一级菜单时，清空二级菜单
            if(this.courseInfo.subjectId){
                this.courseInfo.subjectId = '';
            }
            //获取二级菜单的opotion
            for(let i = 0;i<this.subjectOneList.length;i++){
                if(value == this.subjectOneList[i].id){
                    this.subjectTwoList = this.subjectOneList[i].children;
                }
            }
        },
        init(){

            teacher.getAll()
            .then(response=>{
                this.teacherList = response.data.data;
            })
            subject.getTree()
            .then(response=>{
                this.subjectOneList = response.data.data;

                if(this.$route.params && this.$route.params.id){
                
                    course.info(this.$route.params.id)
                    .then(response=>{
                        //获取二级菜单的opotion
                        for(let i = 0;i<this.subjectOneList.length;i++){
                            if(response.data.info.subjectParentId == this.subjectOneList[i].id){
                                this.subjectTwoList = this.subjectOneList[i].children;
                                this.courseInfo = response.data.info;
                            }
                        }
                        
                    })
                }
            })

            if(this.$route.params && this.$route.params.id){
                this.btnName = "更新并下一步"
            }else{
                this.btnName = "保存并下一步"
                //清空info数据
                this.courseInfo = {
                    cover: '/static/icon.jpg',
                }
            }
            
        }
    },
    created(){
        this.init();
    }
}
</script>

<style scoped>

</style>